<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>averageBy</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const averageBy = (arr, handler) => arr.map(typeof handler === "function" ? handler : val => val[handler]).reduce((acc, val) => acc + val, 0) / arr.length;
        console.log(averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n)); // 5
        console.log(averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n')); // 5
    </script>
</body>

</html>